package com.fs.system.college_admin.service;

import com.fs.system.college_admin.domain.Given;
import com.fs.system.college_admin.mapper.ActiveCreditMapper;
import com.fs.system.college_admin.mapper.ActiveTypeMapper;
import com.fs.system.college_admin.mapper.AdminGivenMapper;
import com.fs.system.student.domain.ApplicationRecord;
import com.fs.system.student.mapper.ApplicationRecordMapper;
import com.fs.system.student.mapper.StudentMapper;
import com.fs.system.teacher.mapper.TeacherMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminGivenService {
    @Autowired
    private AdminGivenMapper adminGivenMapper;

    @Autowired
    private ApplicationRecordMapper applicationRecordMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ActiveTypeMapper activeTypeMapper;

    @Autowired
    private ActiveCreditMapper activeCreditMapper;

    /**
     * 获取一周前的学生申请待分配列表
     * @param stuIdList
     * @param page
     * @param size
     * @return
     */
    public PageInfo<ApplicationRecord> getLatestGiven(List<Integer> stuIdList, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        //用于存储该院下的学待分配的申请记录
        List<ApplicationRecord> allRecords = new ArrayList<>();
        for (Integer stuId : stuIdList) {
            List<ApplicationRecord> stuRecords = applicationRecordMapper.selectGivenRecord(stuId);
            for(ApplicationRecord a:stuRecords){
                a.setStuName(studentMapper.selectStudentById(a.getStuId()).getRealname());
                a.setTypeName(activeTypeMapper.selectActiveTypeById(a.getTypeId()).getName());
                a.setActName(activeCreditMapper.selectActiveCreditById(a.getActId()).getActName());
            }
            if(stuRecords != null && !stuRecords.isEmpty()){
                allRecords.addAll(stuRecords);
            }
        }
        return PageInfo.of(allRecords);
    }

    /**
     * 获取学生最近七天的申请记录待分配列表
     * @param stuIdList
     * @param page
     * @param size
     * @return
     */
    public PageInfo<ApplicationRecord> getGiven(List<Integer> stuIdList, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        //用于存储该院下的学待分配的申请记录
        List<ApplicationRecord> allRecords = new ArrayList<>();
        for (Integer stuId : stuIdList) {
            List<ApplicationRecord> stuRecords = applicationRecordMapper.selectLatestGivenRecord(stuId);
            for(ApplicationRecord a:stuRecords){
                a.setStuName(studentMapper.selectStudentById(a.getStuId()).getRealname());
                a.setTypeName(activeTypeMapper.selectActiveTypeById(a.getTypeId()).getName());
                a.setActName(activeCreditMapper.selectActiveCreditById(a.getActId()).getActName());
            }
            if(stuRecords != null && !stuRecords.isEmpty()){
                allRecords.addAll(stuRecords);
            }
        }
        return PageInfo.of(allRecords);
    }

    /**
     * 院长分配学生申请记录
     * @param recordId
     * @param teaId
     */
    public void givenRecords(Long recordId, Long teaId) {
        ApplicationRecord applicationRecord = applicationRecordMapper.selectRecordById(recordId);
        applicationRecord.setStatus("2");
        applicationRecord.setTeaId(teaId);
        adminGivenMapper.givenRecords(applicationRecord);
    }

    /**
     * 添加分配记录
     * @param given
     */
    public void addGiven(Given given) {
        given.setCreateTime(LocalDateTime.now());
        System.out.println(given+"来吗");
        adminGivenMapper.addOne(given);
    }

    /**
     * 批量分配申请记录
     * @param recordIds
     * @param teaId
     */
    public void givenBatchRecord(List<Long> recordIds, Long teaId) {
        for (Long recordId : recordIds) {
            this.givenRecords(recordId, teaId);
        }
    }
}
